/*
 * Copyright (c) Zhongkehonglue Technologies Co., Ltd. 2025. All rights reserved.
 * Description: Implementation of functions related to the DMC module.
 * Author: wangchunyang
 * Create: 2025-05-23
 */

#ifndef DMC_DEVICE_H
#define DMC_DEVICE_H

#include <string>
#include <vector>
#include <mutex>

#include "dmcParseXml.h"
#include "dmcApiTypes.h"

namespace DlnaDmc {

class DmcDevice {
public:
    DmcDevice(const DmcDevice&) = delete;
    DmcDevice(DmcDevice&&) = delete;
    DmcDevice& operator=(const DmcDevice&) = delete;
    DmcDevice& operator=(DmcDevice&&) = delete;
    dmc_int32 AddDevice(std::shared_ptr<DevDescInfo> &deviceInfo);
    dmc_int32 DeleteDevice(const std::string &udn);
    dmc_int32 DeleteByDeviceId(dmc_uint32 deviceId);
    dmc_int32 ReSet();
    dmc_int32 GetControlUrl(dmc_uint32 deviceId, const std::string &serviceType,
        std::string &controlUrl);
    dmc_int32 GetDescUrl(dmc_uint32 deviceId, std::string &descUrl);
    dmc_int32 GetDeviceIdByUdn(const std::string &udn, dmc_uint32 &deviceId);
    dmc_int32 GetUdnByDeviceId(dmc_uint32 deviceId, std::string &udn);
    dmc_int32 GetAllUdn(std::vector<std::string> &udnVec);
    bool DoesUdnExist(const std::string &udn);
    void ResetHeartBeatCount(dmc_uint32 deviceId);
    dmc_int32 IncreaseHeartBeatCount(dmc_uint32 deviceId, dmc_int32 &failureCount);
    dmc_int32 UpdateLastUpdateTime(const std::string &udn);
    dmc_int32 RemoveOfflineDevice();

    static DmcDevice& GetInstance() {
        return instance;
    }

private:
    DmcDevice() {};
    dmc_uint32 GetNewDeviceId();
    std::shared_ptr<DevDescInfo> FindDeviceByDeviceId(dmc_uint32 deviceId);

    static DmcDevice instance;
    std::unordered_map<std::string, std::shared_ptr<DevDescInfo>> devicesMap;
    std::atomic<dmc_uint32> deviceIdGenerator {1};
    std::mutex devicesMtx;
};

void PeriodicSearchDevice();
}
#endif /* DMC_DEVICE_H */

